package com.olap.starter.common.vo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.olap.starter.common.util.MySqlQueryUtil;
import com.olap.starter.common.util.OracleQueryUtil;
import com.olap.starter.common.util.SqlUtils;
import com.olap.starter.config.dataSource.JdbcTemplateSource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

@Data
@Slf4j
public class DictTableVO {
    String tableName;
    String dbName;
    String label;
    String value;
    String parentValue;
    String defaultValue;
    Map<String, Object> where;

    public String getSQL(JdbcTemplateSource jdbcTemplateSource) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select ");
        String sql2 = null;
        if (StringUtils.isNotBlank(parentValue)) {
            sql.append(parentValue).append(" AS  parentValue,");
        }

        sql.append(label).append(" AS dictLabel,").append(value).append(" AS dictValue from ").append(tableName);
        if (where != null && !where.isEmpty()) {
            sql.append(" where (");
            boolean first = true;
            for (String key : where.keySet()) {
                if (first) {
                    first = false;
                } else {
                    sql.append(" AND ");
                }
                if (key.indexOf("_") == 0) {
                    int end = key.substring(1).indexOf("_");
                    String str = key.substring(1, end + 1);
                    sql.append(key.substring(end + 2)).append(" ").append(str);
                    if ("like".equals(str)) {
                        sql.append(" '%").append(where.get(key)).append("%'");
                    } else if ("in".equals(str)) {
                        sql.append(" (").append(where.get(key)).append(")");
                    } else {
                        sql.append(" '").append(where.get(key)).append("'");
                    }
                } else {
                    sql.append(key).append("  = '").append(where.get(key)).append("'");
                }
            }
            sql.append(")");
            if (defaultValue != null) {
                sql.append(" OR ").append(value).append(" = '").append(defaultValue).append("' ");
            }
        } else if (defaultValue != null) {
            sql2 = sql.toString();
            sql.append(" WHERE ").append(value).append(" != '").append(defaultValue).append("' ").insert(0,"select * from (");
        }
        if (StringUtils.isBlank(parentValue)) {
            String dbType = SqlUtils.getDbType(jdbcTemplateSource.getJdbcTemplate(dbName));
            if (DbType.MYSQL.toString().toUpperCase().equals(dbType.toUpperCase())) {
                sql.append(MySqlQueryUtil.page(0,30));
            }else if(DbType.SQL_SERVER.getDb().toUpperCase().equals(dbType.toUpperCase())){
                sql.append( " ORDER BY (SELECT NULL) ");
                sql.append(OracleQueryUtil.page(0,30));
            }else{
                sql.append(OracleQueryUtil.page(0,30));
            }

        }
        if (defaultValue != null) {
            sql.append(")a");
            StringBuilder newSql = new StringBuilder(sql2).append(" WHERE ").append(value).append(" = '").append(defaultValue).append("' ");
            newSql.append("  union all ");
            newSql.append(sql);
            sql = newSql;
        }
        log.info(sql.toString());
        return sql.toString();
    }
}
